package com.hornsun.manage.commodity.api;

import com.hornsun.client.mall.commodity.api.CommodityAPI;
import com.hornsun.client.mall.commodity.dto.CommodityDTO;
import com.hornsun.domain.dbo.Commodity;
import com.hornsun.domain.dto.FuzzyQueryDTO;
import com.hornsun.repository.CommodityRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

/**
 * Created by 编译中... on 2018/5/7.
 */
@RestController
@RequestMapping("/manage/commodity")
public class CommodityApi {
    @Resource
    private CommodityAPI commodityAPI;

    @Resource
    private CommodityRepository commodityRepository;


    @GetMapping("/getCommodity")
    public ResponseEntity<?> getCommodityInfo(){
        return commodityAPI.getMallInfo();
    }

    @GetMapping("/getMoreCommodity")
    public ResponseEntity<?> getMoreCommodity(@RequestParam("typeid")String typeid , @RequestParam("page")Integer page){
        return commodityAPI.getMoreMallInfo(typeid,page);
    }

    //商品发布
    @Transactional
    @PostMapping("/publish")
    public ResponseEntity<?> publishCommodity(@RequestBody Commodity commodity){
        commodity.setCommodityGuid(UUID.randomUUID().toString());
        commodity.setSales((int)(Math.random()*30+10));
        commodity.setPoint(5.0);
        commodity.setSellTime(new Date());
        commodityRepository.insertCommodity(commodity);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }


    //模糊查询
    @GetMapping("/fuzzyQuery")
    public ResponseEntity<?> fuzzyQuery(@RequestParam("content")String content,@RequestParam("page")int page){
        String description="%"+content+"%";
        FuzzyQueryDTO fuzzyQueryDTO = new FuzzyQueryDTO();
        fuzzyQueryDTO.setList(commodityRepository.fuzzyQuery(description,page));
        fuzzyQueryDTO.setCount(commodityRepository.countByFuzzyQuery(description));
        return new ResponseEntity<Object>(fuzzyQueryDTO,HttpStatus.OK);
    }

    //推荐
    @Transactional
    @PostMapping("/recommend")
    public ResponseEntity<?> recommend(@RequestParam("guid")String commodityGuid){
        Commodity commodity = new Commodity();
        commodity.setCommodityGuid(commodityGuid);
        commodity.setHomepangRecommend((byte)1);
        commodity.setSellTime(new Date());
        commodityRepository.updateCommodity(commodity);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }


    //删除
    @Transactional
    @PostMapping("/delete")
    public ResponseEntity<?> delete(@RequestParam("guid") String commodityGuid){
        commodityRepository.deleteCommodity(commodityGuid);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    //分页获取所有
    @GetMapping("/getAll")
    public ResponseEntity<?> getAll(@RequestParam("page")int page){
        CommodityDTO moreCommodityDTO = new CommodityDTO();
        moreCommodityDTO.setCommodityList(commodityRepository.getALL(page));
        moreCommodityDTO.setTotalCount(commodityRepository.getTotalCount());
        return new ResponseEntity<Object>(moreCommodityDTO,HttpStatus.OK);
    }




}
